//
//  YMSettingViewController.swift
//  YMMedical
//
//  Created by y s on 2023/7/25.
//

import UIKit

class YMSettingViewController: YMBaseViewController {
    
    lazy var tvMain: UITableView = {
        let result = UITableView(frame: .zero, style: .plain)
        result.showsHorizontalScrollIndicator = false
        result.showsVerticalScrollIndicator = false
        result.separatorStyle = .none
        result.delegate = self
        result.dataSource = self
        result.estimatedRowHeight = 0
        result.estimatedSectionFooterHeight = 0
        result.estimatedSectionHeaderHeight = 0
        result.contentInsetAdjustmentBehavior = .never
        result.backgroundColor = .clear
        result.register(YMSettingListCell.self, forCellReuseIdentifier: "YMSettingListCell")
        return result
    }()
    lazy var btnLogout: UIButton = {
        let result = UIButton(type: .custom)
        result.setTitle(YMBridging.kLanguage("logout_txt"), for: .normal)
        result.setTitleColor(SUIUtil.colorOf(0xFF3141), for: .normal)
        result.titleLabel?.font = SFitUtil.fitBoldFont(17)
        result.clipsToBounds = true
        result.layer.cornerRadius = SFitUtil.fit(8)
        result.backgroundColor = STheme.D9;
        result.addTarget(self, action: #selector(logoutClickAction), for: .touchUpInside)
        return result
    }()
    lazy var lblTips: UILabel = {
        let result = SThemeUtil.label("杭州多格科创有限公司提供技术支持", color: STheme.H_D4, fontSize: 12)
        result.textAlignment = .center
        result.numberOfLines = 0
        result.isHidden = true
        return result
    }()
    
    var arrList = Array<Any>()
    var cellLanguage:YMSettingListCell?
    var oldLanguage = YMBridging.getSettingLanguage()

    override func viewDidLoad() {
        super.viewDidLoad()
        updateTextList()
        view.addSubview(tvMain)
        view.addSubview(btnLogout)
        view.addSubview(lblTips)
        tvMain.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
        }
        btnLogout.snp.makeConstraints { make in
            make.bottom.equalTo(SFitUtil.fit(-47)-SFitUtil.safeBottom)
            make.centerX.equalToSuperview()
            make.size.equalTo(SFitUtil.fitSize(345, height: 50))
            make.top.equalTo(tvMain.snp.bottom).offset(SFitUtil.fit(15))
        }
        lblTips.snp.makeConstraints { make in
            make.left.equalTo(SFitUtil.fit(15))
            make.right.equalTo(SFitUtil.fit(-15))
            make.top.equalTo(btnLogout.snp.bottom).offset(SFitUtil.fit(10))
        }
    }
    
    @objc func logoutClickAction() {
        btnLogout.isEnabled = false
        YMLoginService.requestUserLogoutAppSuccess {[weak self] in
            self?.btnLogout.isEnabled = true
        } failure: {[weak self] message in
            self?.btnLogout.isEnabled = true
        }
        YMMyModel.shared().logout()
        self.navigationController?.popViewController(animated: false)
        YMBridging.goAppTab(with: 0)
        YMBridging.goToLogin()
    }
    func updateTextList() {
        showNavTitle(YMBridging.kLanguage("setting_txt"))
        arrList = [["title": YMBridging.kLanguage("alter_pwd_txt"), "subTitle": "", "type":1] as [String : Any],
                   ["title": YMBridging.kLanguage("language_txt"), "subTitle": YMBridging.getSettingLanguage(), "type":6] as [String : Any],
                   ["title":YMBridging.kLanguage("version_info_txt"),"subTitle": "Vi"+SAppUtil.shortVersion,"type":2] as [String : Any],
                   ["title":YMBridging.kLanguage("user_ag_txt"),"subTitle": "","type":3] as [String : Any],
                   ["title":YMBridging.kLanguage("privacy_ag_txt"),"subTitle": "","type":4] as [String : Any],
                   ["title":YMBridging.kLanguage("about_us_txt"),"subTitle": "","type":5] as [String : Any]]
        btnLogout.setTitle(YMBridging.kLanguage("logout_txt"), for: .normal)
        tvMain.reloadData()
    }

}


extension YMSettingViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return arrList.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "YMSettingListCell", for: indexPath) as? YMSettingListCell{
            if let dict = SDataUtil.dictOf(arrList[indexPath.section]) {
                cell.updateData(dict)
            }
            if indexPath.section == 1 {
                cellLanguage = cell
            }
            if indexPath.section == 2 {
                cell.hideArrow(true)
            }else{
                cell.hideArrow(false)
            }
            return cell
        }
        return SThemeUtil.emptyCell;
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return YMSettingListCell.getCellHeight()
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if (section == 0){
            return SFitUtil.fit(15)
        }
        return 0
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return SFitUtil.fit(15)
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let dict = SDataUtil.dictOf(arrList[indexPath.section]){
            let type = SDataUtil.intOf(dict["type"])
            if (type == 1)
            {
                YMBridging.goToChangePwdVC()
            }else if (type == 3)
            {
                YMBridging.go(toProtocolVC: 1)
            }else if (type == 4)
            {
                YMBridging.go(toProtocolVC: 2)
            }else if (type == 5)
            {
                let vc = YMAboutUsViewController()
                SJumpUtil.pushVC(vc, animated: true)
            }else if (type == 6)
            {
                YMBridging.showLanguageSheet(cellLanguage?.lblSub.text ?? "") {[weak self] text in
                    if text == self?.oldLanguage {
                        return
                    }
                    self?.updateTextList()
                }
            }
        }
    }
}
